// house_pkg/pages/locate/index.ts
import QQMapWX from '../../../utils/qqmap'
Page({
  data:{
    address:''
  },
  onLoad(){
    // console.log(1);
    this.getLocation()
  },
  //获取用户当前位置
  async getLocation(){
    wx.showLoading({
      title: '加载中',
    })
    const {latitude,longitude} = await wx.getLocation()
    // console.log(res);
    this.getPoint(latitude,longitude)
  },
  getPoint(x,y){
    //获取用户地址详情
QQMapWX.reverseGeocoder({
  location: `${x},${y}`,
  success:({result:{address}})=>{
    // console.log(address);
    this.setData({address})
  }
})
    //获取周边小区详情
   QQMapWX.search({
    keyword: '住宅小区',  //搜索关键词
    location: `${x},${y}`, 
    page_size:5,
     //设置周边搜索中心点
     success:({data})=>{
      // console.log(data);
      const points = data.map(({id,title,_distance})=>{return {id,title,_distance}})
      // console.log(points);
      this.setData({points})
     },
     complete(){
      wx.hideLoading()
     }
   })
  
  },
  //重新定位
  async chooseLocation(){
   const {latitude,longitude} = await wx.chooseLocation()
   console.log(latitude,longitude);
   this.getPoint(latitude,longitude)
  }
})